﻿using EMoney.IStock.Client.Business.MC;
using EMoney.IStock.Client.DataAccess.Cpyx;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.Cpyx;
using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.Business.Cpyx
{
    /// <summary>
    /// K线英雄任务逻辑类
    /// </summary>
    public class KGameTaskBll
    {
        /// <summary>
        /// 完成K线英雄连续参赛任务
        /// </summary>
        /// <returns></returns>
        public bool CompleteKContinueGameTask(MC_MyInfo myInfo)
        {
            bool flag = false;

            try
            {
                if (myInfo.CompletedDailyTaskID != null 
                    && myInfo.CompletedDailyTaskID.ContainsKey(MC_TaskID.KContinueGame.GetHashCode()))
                {
                    return flag;
                }

                DateTime dtNow = DateTime.Now;
                KContinueGameDal kContinueGameDal = new KContinueGameDal();

                KContinueGame kContinueGame = kContinueGameDal.GetKContinueGame(myInfo.UserID);    //获取连续参加K线英雄游戏信息

                if (kContinueGame == null)
                {
                    kContinueGameDal.AddKContinueGame(new KContinueGame() {
                        UserID = myInfo.UserID,
                        GameDays = 1,
                        GameDate = dtNow.Date
                    });
                }
                else
                {
                    if (kContinueGame.GameDate != dtNow.Date)   //今日未记录连续游戏信息
                    {
                        if (kContinueGame.GameDate != dtNow.AddDays(-1).Date)   //昨天未游戏
                        {
                            kContinueGame.GameDays = 1;
                        }
                        else
                        {
                            if (kContinueGame.GameDays >= 3)
                            {
                                kContinueGame.GameDays = 1;
                            }
                            else
                            {
                                kContinueGame.GameDays = kContinueGame.GameDays + 1;
                            }
                        }

                        kContinueGame.GameDate = dtNow.Date;

                        if (kContinueGameDal.UpdateKContinueGame(kContinueGame) == 1)    //修改连续参加K线英雄游戏信息
                        {
                            if (kContinueGame.GameDays == 3)    //满足任务条件
                            {
                                TaskInfoBll taskInfoBll = new TaskInfoBll();

                                flag = taskInfoBll.CompleteTask(myInfo, MC_TaskID.KContinueGame.GetHashCode());   //完成任务
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(ex, string.Concat("完成K线英雄连续参赛任务失败，userid:", myInfo.UserID));
            }

            return flag;
        }

        /// <summary>
        /// 完成K线英雄收益≥10%的局数任务
        /// </summary>
        /// <returns></returns>
        public bool CompleteKGameCountTask(MC_MyInfo myInfo)
        {
            bool flag = false;

            try
            {
                if (myInfo.CompletedDailyTaskID != null 
                    && myInfo.CompletedDailyTaskID.ContainsKey(MC_TaskID.kGameCount.GetHashCode()))
                {
                    return flag;
                }

                DateTime dtNow = DateTime.Now;
                KGameCountDal kGameCountDal = new KGameCountDal();

                KGameCount kGameCount = kGameCountDal.GetKGameCount(myInfo.UserID, dtNow.Date); //获取收益≥10%的局数

                if (kGameCount == null)
                {
                    kGameCountDal.AddKGameCount(new KGameCount()    //新增收益≥10%的局数
                    {
                        UserID = myInfo.UserID,
                        GameDate = dtNow.Date,
                        GameCount = 1
                    });
                }
                else
                {
                    if (kGameCount.GameCount < 3)
                    {
                        kGameCount.GameCount = kGameCount.GameCount + 1;

                        if (kGameCountDal.UpdateKGameCount(kGameCount) == 1)    //修改收益≥10%的局数
                        {
                            if (kGameCount.GameCount == 3)  //满足任务条件
                            {
                                TaskInfoBll taskInfoBll = new TaskInfoBll();

                                flag = taskInfoBll.CompleteTask(myInfo, MC_TaskID.kGameCount.GetHashCode());   //完成任务
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(ex, string.Concat("完成K线英雄收益≥10%的局数任务失败，userid:", myInfo.UserID));
            }

            return flag;
        }
    }
}
